package com.ruoyi.project.system.visitor.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.common.utils.uniquekey.IdGererateFactory;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import com.ruoyi.project.system.visitor.domain.JiaqinVisitor;
import com.ruoyi.project.system.visitor.mapper.JiaqinVisitorMapper;
import com.ruoyi.project.system.visitor.service.IJiaqinVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 访客Service业务层处理
 *
 * @author LCL
 * @date 2021-09-28
 */
@Service
public class JiaqinVisitorServiceImpl implements IJiaqinVisitorService
{
    @Autowired
    private JiaqinVisitorMapper jiaqinVisitorMapper;
    @Resource
    private IdGererateFactory idGererateFactory;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 查询访客
     *
     * @param id 访客ID
     * @return 访客
     */
    @Override
    public JiaqinVisitor selectJiaqinVisitorById(String id)
    {
        return jiaqinVisitorMapper.selectJiaqinVisitorById(id);
    }

    /**
     * 查询访客列表
     *
     * @param jiaqinVisitor 访客
     * @return 访客
     */
    @Override
    public List<JiaqinVisitor> selectJiaqinVisitorList(JiaqinVisitor jiaqinVisitor)
    {
        return jiaqinVisitorMapper.selectJiaqinVisitorList(jiaqinVisitor);
    }

    /**
     * 新增访客
     *
     * @param jiaqinVisitor 访客
     * @return 结果
     */
    @Override
    public int insertJiaqinVisitor(JiaqinVisitor jiaqinVisitor)
    {
        jiaqinVisitor.setId(UUID.randomUUID().toString().replaceAll("-",""));
        return jiaqinVisitorMapper.insertJiaqinVisitor(jiaqinVisitor);
    }

    /**
     * 修改访客
     *
     * @param jiaqinVisitor 访客
     * @return 结果
     */
    @Override
    public int updateJiaqinVisitor(JiaqinVisitor jiaqinVisitor)
    {
        return jiaqinVisitorMapper.updateJiaqinVisitor(jiaqinVisitor);
    }

    /**
     * 删除访客对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinVisitorByIds(String ids)
    {
        return jiaqinVisitorMapper.deleteJiaqinVisitorByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除访客信息
     *
     * @param id 访客ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinVisitorById(String id)
    {
        return jiaqinVisitorMapper.deleteJiaqinVisitorById(id);
    }

    @Override
    public JiaqinVisitor findByMemberIdAndVisitorMemberId(String memberId, String visitorMemberId) {
        return jiaqinVisitorMapper.findByMemberIdAndVisitorMemberId(memberId, visitorMemberId);
    }

    @Autowired
    private ICompanyService companyService;
    @Override
    public PageInfo<JiaqinVisitor> getMyVisitors(Integer pageNum, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<JiaqinVisitor> list = jiaqinVisitorMapper.getMyVisitors(memberId);
        if(list != null && list.size() != 0) {
            for (JiaqinVisitor JV: list) {
                //关联粉丝用户信息
                Member memberById = memberMapper.selectMemberById(JV.getMemberId());
                Company company = companyService.selectCompanyByMemberId(JV.getMemberId());
                if (memberById != null) {
                    if (company != null) {
                        // 企业
                        memberById.setHeadpic(company.getCompanyAvatar());
                        memberById.setUsername(company.getCompanyName());
                        memberById.setIsCompany(1);
                        memberById.setMemberCompanyId(company.getId());
                    } else {
                        memberById.setIsCompany(0);
                        memberById.setMemberCompanyId(memberById.getId());
                    }
                    JV.setMember(memberById);
                }
            }
        }
        // 去除list中用户信息（memberFollow）为空的数据
//        for (int i = list.size()-1; i >= 0; i--) {
//            if (list.get(i).getMemberFollow() == null) {
//                list.remove(i);
//            }
//        }
        PageInfo<JiaqinVisitor> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<JiaqinVisitor> selectVisitorListByMemberId(String memberId) {
        return jiaqinVisitorMapper.selectVisitorListByMemberId(memberId);
    }
}
